﻿using Jenkins.Common;
using Jenkins.Data;
using Jenkins.UI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.ApplicationSettings;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace Jenkins
{
    public sealed partial class JobItemsPage : LayoutAwarePage
    {
        public JobItemsPage()
        {
            this.InitializeComponent();

            SettingsPane.GetForCurrentView().CommandsRequested += JobItemsPage_CommandsRequested;
            SettingsPane.Show();
        }

        void JobItemsPage_SettingsCommand(IUICommand command)
        {
            if ("jenkinsAuthentication".Equals(command.Id))
            {
                //NavigateTo(SettingsFlyout);
            }
        }

        void JobItemsPage_CommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)
        {
            UICommandInvokedHandler handler = new UICommandInvokedHandler(JobItemsPage_SettingsCommand);
            SettingsCommand authenticationCommand = new SettingsCommand("jenkinsAuthentication", "Authentication", handler);
            args.Request.ApplicationCommands.Add(authenticationCommand);
        }

        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            var sampleDataGroups = JenkinsDataSource.GetGroups((String)navigationParameter);
            this.DefaultViewModel["Groups"] = sampleDataGroups;
        }

        void Header_Click(object sender, RoutedEventArgs e)
        {
            var group = (sender as FrameworkElement).DataContext;
            this.Frame.Navigate(typeof(JobDetailPage), (group as JenkinsViewDataGroup).UniqueId);
        }

        void ItemView_ItemClick(object sender, ItemClickEventArgs e)
        {
            var itemId = (e.ClickedItem as JenkinsJobDataItem).UniqueId;
            this.Frame.Navigate(typeof(JobDetailPage), itemId);
        }
    }
}
